import { BrowserWindow, app, ipcMain, nativeTheme } from 'electron';
import { EVENT } from '../utils/eventTypes';
import { API, UnblockAPI } from '../utils/service';
import { createLogin } from './login';
import { application } from './application';
import { theme } from './theme';
import { netHelper } from '../utils/service';
import { CacheManager } from '../utils/cacheManager';
function getWinFormWebContents(sender: Electron.WebContents) {
  return BrowserWindow.fromWebContents(sender);
}

ipcMain.handle(EVENT.APP_CLOSE, () => {
  app.exit();
});

//#region window平台
ipcMain.handle(EVENT.WINDOW_RESIZ, e => {
  const win = getWinFormWebContents(e.sender);
  if (win.isMaximized()) {
    win.restore();
  } else {
    win.maximize();
  }
});

ipcMain.handle(EVENT.WINDOW_MIN, e => {
  // const win = getWinFormWebContents(e.sender);
  // win.minimize();

  getWinFormWebContents(e.sender).minimize();
});

ipcMain.handle(EVENT.MINIMIZE_TO_TRAY, e => {
  // const win = getWinFormWebContents(e.sender);
  // win.hide();

  getWinFormWebContents(e.sender).hide();
});
//#endregion

ipcMain.handle(EVENT.LOGIN, () => {
  createLogin({ parent: application.win });
});
ipcMain.handle(EVENT.RELOAD_USER, () => {
  return application.win.webContents.executeJavaScript('window.loadUser()');
});

ipcMain.handle(EVENT.WINDOW_SHOW, e => {
  // const win = getWinFormWebContents(e.sender);
  getWinFormWebContents(e.sender).show();
});

ipcMain.handle(EVENT.WINDOW_CLOSE, e => {
  getWinFormWebContents(e.sender).close();
});

ipcMain.handle(EVENT.HTTP, (_, { url, params }) => {
  return API(url, params);
});
ipcMain.handle(EVENT.APP_UNBLOCK, (_, { id, params }) => {
  return UnblockAPI(id, params);
});

ipcMain.handle(EVENT.SAVE_SONG, async (e, song, cookie) => {
  try {
    const win = application.win;
    const res = await API('song_download_url', {
      id: song.id,
      timestamp: Date.now(),
      cookie,
    });
    const url = res.body.data[0].url;
    const artists = song.artists || song.ar;
    const artist = artists.map(it => it.name).join(',');
    application.downloadFileName = `${song.name}-${
      artist + url.substring(url.lastIndexOf('.'))
    }`;
    win.webContents.downloadURL(url);
  } catch (error) {
    return { error };
  }
  // console.log('download:  ', song);

  // downloadMusic('./', song);
});
ipcMain.handle(EVENT.DARK_MODE_TOGGLE, e => {
  theme.toggleTheme();
  return theme.useDark;
});
ipcMain.handle(EVENT.DARK_MODE_SYSTEM, theme.useSystemTheme);

ipcMain.handle(EVENT.APP_IS_DARK, () => {
  return theme.useDark;
});
// 修改title
ipcMain.handle(EVENT.SET_TITLE, (e, title?: string) => {
  if (title) {
    application.tray.setToolTip(title);
    application.win.setTitle(title);
  }
});

ipcMain.handle(EVENT.WEB_AUDIO_TOGGLE_PLAY, (e, play: boolean) => {
  application.thumbar.togglePlay(play);
});

ipcMain.handle(EVENT.APP_NET_STATUS, () => {
  return netHelper.checkInternetConnection();
});

//#region 缓存
ipcMain.handle(EVENT.APP_MAX_CACHE_SIZE, async () => {
  const size = await CacheManager.getInstance().getMaxCacheSize();
  return size;
});
ipcMain.handle(EVENT.APP_CACHE_SIZE, async () => {
  const usedSize = await CacheManager.getInstance().getCacheSize();
  const maxSize = await CacheManager.getInstance().getMaxCacheSize();
  return { usedSize, maxSize };
});

ipcMain.handle(EVENT.APP_CACHE_CLEAR, async () => {
  await CacheManager.getInstance().clearCache();
});

//#endregion
